package cn.zw.msmp.controller;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.zw.msmp.common.util.CommonUtils;
import cn.zw.msmp.controller.vo.BaseVO;
import cn.zw.msmp.pojo.TransactionInfo;
import cn.zw.msmp.service.ITransactionService;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping("/transaction/")
public class TransactionController {
	
	private static final Logger log = LogManager.getLogger(TransactionController.class);
	
	private int page = 1;
	private int rows = 20;
	
	@Autowired
	@Qualifier("transactionService")
	private ITransactionService service;
	
	@RequestMapping("/add")
	public String status(@RequestBody String condition) {
		try {
			if(StringUtils.isNotEmpty(condition)) {
				Map<String, String> map = CommonUtils.convertReqParams(condition);
				TransactionInfo info = JSON.parseObject(JSON.toJSONString(map), TransactionInfo.class);
				service.insert(info);
			}
		} catch (Exception e) {
			log.error("add transaction info error", e);
			return "-1";
		}
		return "0";
	}
	
	/**
	 * 删除交易记录
	 * @param ids
	 * @return 成功删除的记录数
	 */
	@RequestMapping("/delete")
	public int delete(@RequestBody String ids) {
		try {
			ids = URLDecoder.decode(ids.replaceAll("=", ""), "UTF-8");
			List<Integer> list = new ArrayList<>();
			String[] tmp = ids.split(",");
			for(String id : tmp) {
				list.add(Integer.valueOf(id));
			}
			return service.delete(list);
		} catch (Exception e) {
			log.error("delete transaction info error", e);
			return -1;
		}
	}
	
	@RequestMapping("/update")
	public String update(@RequestBody String condition) {
		try {
			if(StringUtils.isNotEmpty(condition)) {
				Map<String, String> map = CommonUtils.convertReqParams(condition);
				TransactionInfo info = JSON.parseObject(JSON.toJSONString(map), TransactionInfo.class);
				service.update(info);
			}
		} catch (Exception e) {
			log.error("update transaction info error", e);
		}
		return "0";
	}
	
	@RequestMapping("/list")
	public BaseVO<List<TransactionInfo>> query(@RequestBody String condition) {
		TransactionInfo queryInfo = new TransactionInfo();
		if(StringUtils.isNotEmpty(condition)) {
			Map<String, String> map = CommonUtils.convertReqParams(condition);
			queryInfo = JSON.parseObject(JSON.toJSONString(map), TransactionInfo.class);
			page = StringUtils.isNotEmpty(map.get("page")) ? Integer.parseInt(map.get("page")) : page;
			rows = 20; 
		}
		PageInfo<TransactionInfo> infos = service.queryTransactions(queryInfo, page, rows);
		List<TransactionInfo> list = infos.getList();
		rows = (int) (null != infos ? infos.getTotal() : rows);
		BaseVO<List<TransactionInfo>> result = new BaseVO<List<TransactionInfo>>(page, rows, infos.getTotal(), list);
		return result;
	}
}
